//
//  Group.m
//  MHO
//
//  Created by papo on 4/12/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "Group.h"
#import "Lib.h"
#import "Contants.h"
#import "ServiceLib.h"
#import "SBJson.h"

@implementation Group

+ (NSMutableArray*)getAllGroups {
    return [self getAllGroupsSystem:@"0"];
}

+ (NSMutableArray*)getAllGroupsSystem:(NSString *)system {
    NSMutableArray* groups = [[NSMutableArray alloc]init];
    NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/cat?IdNhom=%@&IdSystem=2&key=%@", system, kServerKey];

    NSString* retVal = [ServiceLib sendGetRequest:url];

    if (retVal) {
        SBJsonParser* parser = [[SBJsonParser alloc] init];
        id ret = [parser objectWithString:retVal];
        if (ret && [ret isKindOfClass:[NSDictionary class]]) {
            if ([ret objectForKey:@"Ret"] && [[ret objectForKey:@"Ret"] intValue] > 0) {
                NSArray* data = [ret objectForKey:@"Data"];
                if (data) {
                    for (NSDictionary* dict in data) {
                        if (dict && [dict isKindOfClass:[NSDictionary class]]) {
                            Group* g = [Group groupFromDictionary:dict];
                            if (g) {
                                [groups addObject:g];
                            }
                        }
                    }
                }
            }
        }
    }

    return groups;
}

+ (Group*)groupFromDictionary:(NSDictionary *)dictionary {
    return [[self alloc]initWithDictionary:dictionary];
}

- (id)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        self.groupId = numberCheckNull([dictionary objectForKey:@"Id"]);
        self.name = strCheckNull([dictionary objectForKey:@"Name"]);
//        self.description = strCheckNull([dictionary objectForKey:@"Description"]);
        self.background = strCheckNull([dictionary objectForKey:@"Backround"]);
        self.status = strCheckNull([dictionary objectForKey:@"Status"]).boolValue;
        self.imageUrl = strCheckNull([dictionary objectForKey:@"Image"]);
        self.views = numberCheckNull([dictionary objectForKey:@"View"]);
        self.count = numberCheckNull([dictionary objectForKey:@"SoLuong"]);
    }
    return self;
}
@end
